<template>
  <div id="wrap">
    <div class="login" >
      <h2>账号注册</h2>
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="账号" prop="age">
          <el-input v-model.number="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>

        </el-form-item>
        <el-form-item>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
          <router-link to="/">
            <el-button>返回</el-button>
          </router-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from "axios";

export default {
  data() {
    var checkAge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('账号不能为空'));
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        password: '',
        checkPass: '',
        username: ''
      },
      rules: {
        password: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        username: [
          { validator: checkAge, trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$forceUpdate();
          this.register();
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    register(){
      axios.post('http://localhost:8080/User/Reg',this.ruleForm)
          .then(response => {
            if(response.data.code==200) {
              this.$message.success("注册成功");
              this.$router.push("/");
            }
            else {
              this.$message.error("注册失败,用户已存在");
              this.fetchDataAndUpdateCache();
            }
          })
          .catch(error => {
            console.error('Error adding new item:', error);
            this.$message.error("注册失败");
            this.fetchDataAndUpdateCache();
          });
    },
    fetchDataAndUpdateCache(){
      this.ruleForm= {
        password: '',
        checkPass: '',
        username: ''
      }
    }
  }
}
</script>
<style scoped>

#wrap {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.login {
  width: 460px;
  padding: 40px;
  background-color: rgb(31, 41, 55);
  box-shadow: 0 15px 25px rgba(0, 0, 0,.6);
  border-radius: 10px;
  color: #fff;
  --c1: #03e9f4;
}
</style>